package com.woniuxy.house.web;

import com.woniuxy.house.entity.Post;
import com.woniuxy.house.entity.Source;
import com.woniuxy.house.mapper.PostMapper;
import com.woniuxy.house.mapper.SourceMapper;
import com.woniuxy.house.utils.JwtUtil;
import com.woniuxy.house.vo.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 基于关注界面的三个功能
 */
@RestController
@RequestMapping("/api/follow")
@SuppressWarnings("all")
public class FollowController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private SourceMapper sourceMapper;//这里没有看到service的实现 暂时先用mapper接口  后面可以改

    @Autowired
    private PostMapper postMapper;

    /**
     * 功能1：显示用户所关注的房源
     * @param id
     * @return
     */
    @RequestMapping("/source")
    //通过用户的id 查找用户所关注的所有房源
    public ResponseEntity<Source> getSource(@RequestHeader String jwt){
        if(jwtUtil.verifyJWT(jwt)) {
            Integer id = (Integer) jwtUtil.verifyJWTGetUserId(jwt, "id");
            List<Source> sources = sourceMapper.getByUserId(id);//得到关注房源集合
            return new ResponseEntity("200", "成功", sources);
        }else{
            return  ResponseEntity.FAIL;
        }
    }




    /**
     * 功能2：显示帖子(求租贴)
     * @return
     */
    @RequestMapping("/rent")

    public ResponseEntity<Post> getRent(){
        List<Post> rents = postMapper.getQiuZuAll();//得到所有求租贴
        return  new ResponseEntity("200","成功",rents);
    }

    /**
     * 功能2：显示帖子(找室友贴)
     * @return
     */
    @RequestMapping("/mate")
    public ResponseEntity<Post> getMate(){
        List<Post> mates = postMapper.getZhaoZuAll();//得到所有找室友贴
        return  new ResponseEntity("200","成功",mates);
    }

}
